<?php

/**
 * @author Rizky Tahara Shita
 * @website http://tahara.web.id
 * @email rizky.tahara@gmail.com
 * @copyright b'IT Cyber Solution
 */

class HRIS_M_live_data extends BZ_Model
{
/**
 * @section HRIS::Live Data Model
 */

	/**
	 * @function combo_fill()
	 * @description fill the dropdown with recursive or normal data
	 */
	public function combo_fill($prm_dbase = 'db_default', $prm_module_table = '', $prm_object_start = '', $prm_type = 'NORMAL', $prm_where = array(), $prm_root_show = TRUE, $prm_ajax_returned_value = '', $prm_choose_show = TRUE, $prm_query_special = FALSE)
	{
		/**
		 * @description $prm_module_table:
		 * 								if as a string, then process regular data (normal / recursive)
		 * 								if as array, then the index should be ID and NAME;
		 * 									the purpose is to get the module_id and module_name (type) from the hris_master_data
		 * 									where the module_table__id need to be shown as recursive --see the case 'master_data' below :)
		 */

		switch ($prm_type)
		{
			case 'RECURSIVE':
				/**
				 * @description get the data for the recursive combo
				 */
				$table = array
					(
						'NAME' => 'hris_master_data'
						, 'PRIMARY' => array('SHOW' => FALSE, 'DATA' => array('id'))
						, 'FIELDS' => array('NAME' => array('id', 'name'), 'CAPTION' => 'name', 'ORDER' => 'id', 'PARENT_NAME' => 'parent_id')
						, 'ROOT_SHOW' => $prm_root_show
					);


				is_array($prm_module_table)
					? $prm_module_table_name = $prm_module_table['NAME']
					: $prm_module_table_name = $prm_module_table;

				switch ($prm_module_table_name)
				{
					case 'master_data':
						$table['FIELDS']['NAME'] = array('@_id := id as id', '`fn_hris_master_data_get_name`('.$prm_module_table['ID'].', @_id, "'.$this->session->userdata('session_user_language_id').'") as name');
						switch ($prm_module_table['ID'])
						{
							case 22:
							case 29:
								$table['FIELDS']['ORDER'] = 'name';
								break;
						}
						break;

					case 'area':
						$table['NAME'] = 'hris_master_area';
						break;

					case 'company_profile':
						$table['NAME'] = 'hris_organization_company';
						break;

					case 'company_work_location':
						$table['NAME'] = 'hris_organization_company_work_location';
						$table['FIELDS']['NAME'] = array('id', 'name');
						$table['FIELDS']['CAPTION'] = 'name';
						break;
/*
					case 'company_position':
						$table['NAME'] = 'hris_organization_company_position';
						$table['FIELDS']['NAME'] = array('id', 'code');
						$table['FIELDS']['CAPTION'] = 'code';
						break;
*/
					case 'cost_center':
						$table['NAME'] = 'hris_organization_company_cost_center';
						$table['FIELDS']['NAME'] = array('id', 'code');
						$table['FIELDS']['CAPTION'] = 'code';
						break;

					case 'job_specification':
						$table['NAME'] = 'hris_master_data';
						$table['FIELDS']['NAME'] = array('@_id := id as id', '`fn_hris_master_data_get_name`(29, @_id, "'.$this->session->userdata('session_user_language_id').'") as name');
						$table['FIELDS']['CAPTION'] = 'name';
						break;

					case 'company_structure':
						$table['NAME'] = 'hris_organization_company_structure';
						$table['FIELDS']['NAME'] = array('id', 'name');
						break;
/*
					case 'attendance_status':
						$table['NAME'] = 'hris_attendance_status';
						$table['FIELDS']['NAME'] = array('id', 'name');
						break;
*/
				}


				/**
				 * @description load the models
				 */
				$this->load->model('M_viewlist');


				$return_value = $this->M_viewlist->generate
					(
						$prm_dbase
						, $table['FIELDS']['NAME'], $table['PRIMARY'], $table['FIELDS']['CAPTION'], $table['FIELDS']['ORDER']
						, $table['NAME']
						, $prm_where
						, ''
						, $this->bz_segment['URL']
						, 0
						, 'form'
						, FALSE
						, FALSE
						, 'none'
						, 'NO_HISTORY'
						, FALSE, ''
						, TRUE
						, $table['FIELDS']['PARENT_NAME']
						, TRUE
						, 0, 0
						, '&mdash;', $table['ROOT_SHOW']
						, FALSE
						, $prm_query_special
					);

				/**
				 * @description clean up the recursive structure
				 */
				unset($this->M_viewlist->return_recursive);
				$this->M_viewlist->return_recursive[0] = '/root';
				break;


			default:
				/**
				 * @description get the data for normal combo
				 */
				if (is_numeric($prm_module_table))
				{
					$table = array
						(
							'NAME' => 'hris_master_data'
							, 'WHERE' => array_merge
								(
									$prm_where
									, array('module_table__id' => $prm_module_table)
								)
							, 'FIELDS' => '@_id := id as combo_key, `fn_hris_master_data_get_name`('.$prm_module_table.', @_id, "'.$this->session->userdata('session_user_language_id').'") as combo_value'
						);


					switch ($prm_module_table)
					{
						case 33:
						case 39:
						case 43:
							$table['ORDER'] = 'combo_key';
							break;

						default:
							$table['ORDER'] = 'combo_value';
							break;
					}
				}
				else
				{
					$table = array
						(
							'NAME' => $prm_module_table
							, 'WHERE' => $prm_where
							, 'FIELDS' => 'id as combo_key, name as combo_value'
							, 'ORDER' => 'combo_value'
						);

					switch ($prm_module_table)
					{
						case 'hris_organization_company_position_class_grade':
						case 'hris_organization_company_structure_position_grade':
							$table['FIELDS'] = '@_id := position_grade__id as combo_key, `fn_hris_master_data_get_name`(33, @_id, "'.$this->session->userdata('session_user_language_id').'") as combo_value';
							$table['ORDER'] = 'combo_key';
							break;

						case 'hris_employee_leave_management':
						case 'hris_attendance_leave_management_company':
							$table['FIELDS'] = '@_leave_management_id := leave_management__id as combo_key, (select case request_type__id when 218 then concat(name, " (", day_type, ")") else name end from bz_hris_attendance_leave_management where id = @_leave_management_id) as combo_value';
							break;

						case 'hris_employee_data':
							$table['FIELDS'] = '@_id := id as combo_key, ';
							if (isset($prm_where['EMPLOYEE_NO_DATA_ID']))
							{
								$table['FIELDS'] .= 'name_official as combo_value';
								$table['WHERE'] = $prm_where['DATA'];
							}
							else
							{
								$table['FIELDS'] .= 'concat(`fn_hris_employee_number_current`(id), " - ", name_official) as combo_value';
							}
							break;

						case 'hris_attendance_shift_daily':
							$table['FIELDS'] = 'id as combo_key, code as combo_value';
							break;
					}
				}


				$return_value = $this->M_database->datas
					(
						$prm_dbase
						, $table['NAME'], $table['WHERE'], $table['FIELDS']
						, FALSE
						, $table['ORDER']
						, ''
						, '', ''
						, $prm_query_special
						, FALSE, ''
						, TRUE
					);
				break;
		}


		if ($prm_choose_show == TRUE)
		{
			$return_value = array('' => $this->lang->line('hris_choose')) + $return_value;
		}

		if (trim($prm_object_start) != '')
		{
			/**
			 * @description put the data via ajax!
			 */
			$return_value = array('js_action_combo_item_add', $return_value, $prm_object_start, $prm_ajax_returned_value);
		}


		return $return_value;
	}


	/**
	 * @function combo_group()
	 * @description generate the dropdown group
	 */
	public function combo_group($prm_dbase = 'db_default', $prm_module_table = '', $prm_parent_id = 0)
	{
		if (is_numeric($prm_module_table))
		{
			$table_where = array('module_table__id' => $prm_module_table);
			$table = array
				(
					'NAME' => 'hris_master_data'
					, 'FIELDS' => '@_id := id as id, `fn_hris_master_data_get_name`('.$prm_module_table.', @_id, "'.$this->session->userdata('session_user_language_id').'") as name'
					, 'WHERE' => array_merge
						(
							$table_where
							, array('parent_id' => $prm_parent_id)
						)
					, 'ORDER' => 'id'
					, 'LIMIT' => $this->web_data['BISKITZ']['SETTINGS']['BISKITZ_VIEW_TOT_ROW']
				);

			switch ($prm_module_table)
			{
				case 29:
#				case 33:
					$table['ORDER'] = 'name';
					break;

				case 33:
					$table['LIMIT'] = '';
					break;
			}
		}
		else
		{
			$table_where = $prm_module_table['WHERE'];
			$table = array
				(
					'NAME' => $prm_module_table['NAME']
					, 'FIELDS' => $prm_module_table['FIELDS']
					, 'WHERE' => array_merge
						(
							$prm_module_table['WHERE']
							, array('parent_id' => $prm_parent_id)
						)
					, 'ORDER' => $prm_module_table['ORDER']
					, 'LIMIT' => $this->web_data['BISKITZ']['SETTINGS']['BISKITZ_VIEW_TOT_ROW']
				);
		}


		$result = $this->M_database->datas($prm_dbase, $table['NAME'], $table['WHERE'], $table['FIELDS'], FALSE, $table['ORDER'], '', $table['LIMIT']);
		if (is_array($result))
		{
			foreach ($result as $data)
			{
				$sub_count = $this->M_database->total_rows($prm_dbase, $table['NAME'], array_merge($table_where, array('parent_id' => $data['id'])));
				if ($sub_count > 0)
				{
					$return_value[$data['name']] = $this->combo_group($prm_dbase, $prm_module_table, $data['id']);
				}
				else
				{
					$return_value[$data['id']] = $data['name'];
				}
			}
		}


		return $return_value;
	}


	/**
	 * @function combo_data_set()
	 * @description set the multipe data
	 */
	public function combo_data_set($prm_dbase = 'db_default', $prm_table = array(), $prm_data = array())
	{
		$use_db = $this->{$prm_dbase};

		$use_db->delete($prm_table['NAME'], $prm_table['WHERE']);
		foreach ($prm_data as $data)
		{
			$this->M_database->insert($prm_dbase, $prm_table['NAME'], $data, FALSE, FALSE);

			$this->M_log->write
				(
					$prm_dbase
					, sprintf
						(
							$this->lang->line('log_form_insert')
							, $this->router->class, 'modal', json_encode($data)
						)
				);
		}
	}


	/**
	 * @function combo_data_get()
	 * @description get the multiple data
	 */
	public function combo_data_get($prm_dbase = 'db_default', $prm_table = array())
	{
		$return_value = array();


		$use_db = $this->{$prm_dbase};
		$qry = $use_db->get_where($prm_table['NAME'], $prm_table['WHERE']);

		if ($qry->num_rows() > 0)
		{
			foreach ($qry->result_array() as $rows)
			{
				$return_data[] = $rows[$prm_table['FIELDS']];
			}

			$return_value[] = array($return_data);
		}


		return $return_value;
	}


	/**
	 * @function master_data_reference_get
	 * @description get the master data reference
	 */
	public function master_data_fill_type_get($prm_module_table = '', $prm_key = '')
	{
		$this->db_default->select('reference_id, `fn_hris_master_data_get_name`('.$prm_module_table.', reference_id, "'.$this->session->userdata('session_user_language_id').'") as reference_name', FALSE)
			->from('hris_master_data')
			->where(array('id' => $prm_key));
		$query = $this->db_default->get();

		$return_value = array($query->row()->reference_id, $query->row()->reference_name);


		return $return_value;
	}


	/**
	 * @function combo_year
	 * @description generate the year in combo
	 */
	public function combo_year($prm_start = '1980', $prm_end = '')
	{
		if (trim($prm_end) == '')
		{
			$prm_end = date('Y');
		}

		for ($i = $prm_start; $i <= $prm_end; $i++)
		{
			$return_value[$i] = $i;
		}

		return $return_value;
	}



/**
 * @section COMPANY STRUCTURE
 * @description company structure functions
 */

	/**
	 * @function _company_structure_position
	 * @description generate the position
	 */
	private function _company_structure_position($prm_dbase = 'db_default', $prm_company_id = '', $prm_parent_id = 0, $prm_job_title_id = '')
	{
		$structure = array();

		if (trim($prm_company_id) != '')
		{
			$use_db = $this->{$prm_dbase};

			(trim($prm_parent_id) == '') ? $query_where = array() : $query_where = array('parent_id' => $prm_parent_id);

			if (trim($prm_job_title_id) != '')
			{
				$query_where = array_merge($query_where, array('id in ' => '(select structure__id from bz_hris_organization_company_structure_position where job_title__id = ' . $prm_job_title_id . ')'));
			}


			$use_db->where(array_merge(array('company__id' => $prm_company_id, 'status' => '"Active"'), $query_where), NULL, FALSE);
			$query = $use_db->get('hris_organization_company_structure');
			if ($query->num_rows() > 0)
			{
				foreach ($query->result() as $rows)
				{
					if ($rows->structure_level__id == 169)
					{
						$structure[$rows->id] = $rows->name;

						$menu_sub_count = $this->M_database->total_rows
							(
								$prm_dbase
								, 'hris_organization_company_structure'
								, array_merge(array('company__id' => $prm_company_id, 'status' => '"Active"'), $query_where)
								, TRUE
							);
						if ($menu_sub_count > 0)
						{
							$structure[$rows->parent_id] = $this->_company_structure_position($prm_dbase, $prm_company_id, $rows->id, $prm_job_title_id);
						}
					}
					else
					{
						$structure[$rows->id] = $this->_company_structure_position($prm_dbase, $prm_company_id, $rows->id, $prm_job_title_id);
					}
				}
			}
#			else
#			{
#				$this->company_structure_position($prm_dbase, $prm_company_id, $prm_parent_id, $prm_job_title_id);
#			}
		}


		return $structure;
	}


	/**
	 * @function company_structure_position_by_work_location
	 * @description get the position structure based on work location
	 */
	public function company_structure_position_by_work_location($prm_dbase = 'db_default', $prm_company_id = '', $prm_work_location_id = '')
	{
		$structure = array();

		if (trim($prm_company_id) != '' && trim($prm_work_location_id) != '')
		{
			$use_db = $this->{$prm_dbase};

			$query = $use_db->get_where('hris_organization_company_structure_work_location', array('work_location__id' => $prm_work_location_id));
			if ($query->num_rows() > 0)
			{
				foreach ($query->result() as $rows)
				{
					$structure[$rows->structure__id] = $this->_company_structure_position($prm_dbase, $prm_company_id, $rows->structure__id);
					if (count($structure[$rows->structure__id]) == 0)
					{
						unset($structure[$rows->structure__id]);
					}
				}
			}
		}


		return $structure;
	}


	/**
	 * @function company_structure_position
	 * @description get the position structure
	 */
	public function company_structure_position($prm_dbase = 'db_default', $prm_company_id = '', $prm_structure_id = '', $prm_job_title_id = '')
	{
		$structure = array();

		if (trim($prm_company_id) != '')
		{
			$use_db = $this->{$prm_dbase};

			(trim($prm_structure_id) == '') ? $query_where = array() : $query_where = array('id' => $prm_structure_id);
			$query = $use_db->get_where('hris_organization_company_structure', array_merge(array('company__id' => $prm_company_id, 'status' => 'Active'), $query_where));
			if ($query->num_rows() > 0)
			{
				foreach ($query->result() as $rows)
				{
					$structure[$rows->id] = $this->_company_structure_position($prm_dbase, $prm_company_id, $rows->id, $prm_job_title_id);
					if (count($structure[$rows->id]) == 0)
					{
						unset($structure[$rows->id]);
					}
				}
			}
		}


		return $structure;
	}



}

/** End of file HRIS_M_live_data.php */
/** Location: ./biskitz_mpc/biskitz/models/admin/hris/hris_m_live_data.php */
